package com.xlh.service.impl;
import com.xlh.dao.ProductDao;
import com.xlh.domain.Product;
import com.xlh.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;
    @Override
    public Product findByPid(Integer pid) {
        return productDao.findById(pid).get();
    }
    @Override
    @Transactional
    public void reduceInventory(Integer pid, int num) {
        //查询商品
        Product product = productDao.findById(pid).get();
        //省略校验
        product.setStock(product.getStock() - num);
        Random random = new Random();
        System.out.println(1/random.nextInt(2));
        //减库存
        productDao.save(product);
    }
}
